<template>
	<view class="main">
		<view class="list" v-for="(item,index) in list" :key="index2">
			<view class="flex align between info">
				<view class="info-item">
					<view>{{item.merge_keyword}}</view>
					<view>类型：{{item.keyword_type}}</view>
					<view>添加时间：{{item.addtime}}</view>
				</view>
				<uni-icons type="trash" size="20" color="#D81E06" @click="toDel(item.id)"></uni-icons>
			</view>
		</view>
		<xw-empty :isShow="empty" text="暂无数据" textColor="#777777"></xw-empty>
		<uni-load-more :status="status" v-if="!empty" :content-text="loadText"></uni-load-more>
	</view>
</template>

<script>
	import {
		mergeKeywordIndex,
		mergeKeywordAdd,
		mergeKeywordDel
	} from '@/api/apiData.js';
	export default {
		data() {
			return {
				totalPage: 1,
				page: 0,
				offset: 20,
				list: [],
				scrollTop: 0,
				empty: false,
				loadText: {
					loadmore: '点击或上拉加载更多',
					loading: '努力加载中',
					nomore: '实在没有了'
				},
				status: 'loadmore',
				id:''
			};
		},
		onLoad(option) {
			this.id = option.id
			// console.log(decodeURIComponent('%7B%22merge_keyword%22%3A%22%E4%BA%91%E5%AA%92%22%7D'))
			this.getlist();
		},
		onPageScroll(e) {
			const that = this;
			that.scrollTop = e.scrollTop;
		},
		onReachBottom() {
			const that = this;
			that.getlist();
		},
		methods:{
			// 删除
			toDel(id){
				let that = this
				uni.showModal({
					title: "提示",
					content: "确定要删除吗？",
					showCancel: true,
					success: res => {
						if (res.confirm) {
							that.delCom(id)
						} else {
							
						}
					}
				
				})
			},
			async delCom(id){
				let obj = {
					action: 'del',
					ids: id
				}
				const result = await mergeKeywordDel(obj)
				if(result.code==1){
					uni.showToast({
						title:'删除成功'
					})
					this.show = false
					this.page = 0
					this.list = []
					this.getlist()
				}else{
					uni.showToast({
						title:result.msg,
						icon:'none'
					})
				}
			},
			async toAdd(){
				if(this.keyword==''){
					uni.showToast({
						title:'请输入关键字',
						icon:'none'
					})
					return false
				}
				let obj = {
					'row[merge_keyword]':this.keyword
				}
				const result = await mergeKeywordAdd(obj)
				if(result.code==1){
					uni.showToast({
						title:'添加成功'
					})
					this.show = false
					this.page = 0
					this.list = []
					this.getlist()
				}else{
					uni.showToast({
						title:result.msg,
						icon:'none'
					})
				}
			},
			change(){
				this.page = 0
				this.list = []
				this.getlist()
			},
			async getlist() {
				const that = this;
				const {
					totalPage,
					page,
					offset,
					list,
					group_id,
					type,
					merge_keyword,
					id
				} = that;
			
				let obj = {
					menu_url:'merge_keyword',
					limit: offset,
					offset: page,
					sort: 'id',
					order: 'desc',
					group_ids:id,
					_:new Date().getTime()
					// filter:encodeURIComponent(JSON.stringify(filter)),
					// op:encodeURIComponent(JSON.stringify(filter))
				};
				that.status = 'loading';
				if (totalPage > page) {
					let result = await mergeKeywordIndex(obj);
					that.page = page + 1;
					if (result.total == 0) {
						that.empty = true;
						that.status = 'nomore';
						return;
					}
					that.empty = false;
					setTimeout(() => {
						that.status = 'loadmore';
					}, 500);
					that.list = list.concat(result.rows);
					that.totalPage = Math.ceil(result.total / offset);
				} else {
					setTimeout(() => {
						that.status = 'nomore';
					}, 500);
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.main {
		margin: 13rpx 30rpx;

		.list {
			background: white;
			box-shadow: 1rpx 1rpx 5rpx 1rpx gainsboro;
			border-radius: 15rpx;
			margin-bottom: 30rpx;

			.name {
				margin: 0 17rpx;
				padding: 26rpx 15rpx 20rpx;
				border-bottom: solid 1rpx #D6D6D6;
				background: #2D2D2D;
				color: #FFE9E0;
				border-radius: 15rpx 15rpx 0rpx 0rpx;

				view:nth-child(1) {
					font-size: 28rpx;
					font-weight: bold;
				}

				view:nth-child(2) {
					font-size: 18rpx;
				}
			}

			.info {
				font-size: 20rpx;
				padding: 14rpx 41rpx;

				image {
					width: 108rpx;
					height: 108rpx;
				}

				.info-item {
					width: 70%;
					font-size: 20rpx;

					view {
						margin-bottom: 4rpx;
					}

					view:nth-child(1) {
						font-size: 32rpx;
						font-weight: bold;
						margin-bottom: 15rpx;
					}
				}
			}

		}
	}
</style>